package team.hyznrj.studentsys.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import team.hyznrj.studentsys.converter.CourseAchievement2CourseDto;
import team.hyznrj.studentsys.converter.CourseAchievementForm2CourseAchievement;
import team.hyznrj.studentsys.dao.mapper.CourseAchievementMapper;
import team.hyznrj.studentsys.dao.repository.AcademicYearsRepository;
import team.hyznrj.studentsys.dao.repository.CourseAchievementRepository;
import team.hyznrj.studentsys.dao.repository.CurriculumNatureRepository;
import team.hyznrj.studentsys.dto.CourseDto;
import team.hyznrj.studentsys.entity.AcademicYears;
import team.hyznrj.studentsys.entity.CourseAchievement;
import team.hyznrj.studentsys.entity.CurriculumNature;
import team.hyznrj.studentsys.form.CourseAchievementForm;
import team.hyznrj.studentsys.form.CourseConditionalForm;
import team.hyznrj.studentsys.service.CourseAchievementService;

import java.util.Arrays;
import java.util.List;

/**
 * @author ReMidDream
 * @date 2018-03-27 13:21
 **/
@Service
@Slf4j
public class CourseAchievementImpl implements CourseAchievementService {

    @Autowired
    private CourseAchievementRepository courseAchievementRepository;
    @Autowired
    private CourseAchievementMapper courseAchievementMapper;
    @Autowired
    private CourseAchievementForm2CourseAchievement courseAchievementConverter;
    @Autowired
    private CourseAchievement2CourseDto courseDtoConverter;
    @Autowired
    private CurriculumNatureRepository curriculumNatureRepository;
    @Autowired
    private AcademicYearsRepository academicYearsRepository;

    public CourseAchievementImpl(CourseAchievementRepository courseAchievementRepository, CourseAchievementMapper courseAchievementMapper,
                                 CourseAchievementForm2CourseAchievement courseAchievementConverter, CourseAchievement2CourseDto courseDtoConverter,
                                 CurriculumNatureRepository curriculumNatureRepository, AcademicYearsRepository academicYearsRepository) {
        this.courseAchievementRepository = courseAchievementRepository;
        this.courseAchievementMapper = courseAchievementMapper;
        this.courseAchievementConverter = courseAchievementConverter;
        this.courseDtoConverter = courseDtoConverter;
        this.curriculumNatureRepository = curriculumNatureRepository;
        this.academicYearsRepository = academicYearsRepository;
    }

    public List<CourseDto> findByConditional(CourseConditionalForm courseConditionalForm){

        //判断是否存在学号这个查询条件
        if (courseConditionalForm.getStudentId()!=null) {
            //将多个学号拆分进List
            String[] split = courseConditionalForm.getStudentId().split(",");
            List<String> studentIds = Arrays.asList(split);
            courseConditionalForm.setStudentIds(studentIds);
        }
        //查询学生成绩并进行动态拼接
        List<CourseAchievement> result = courseAchievementMapper.findByCourseAchievementForm(courseConditionalForm);
        return courseDtoConverter.converter(result);
    }

    @Override
    @Transactional
    public int insertOrUpdateCourseAchievement(List<CourseAchievementForm> courseAchievementForm) {
        List<CourseAchievement> converter = courseAchievementConverter.converter(courseAchievementForm);
        return courseAchievementRepository.save(converter).size();
    }

    @Override
    public boolean insertOrUpdateCourseAchievement(CourseAchievement courseAchievement) {
        //查询课程性质和学年表
        List<CurriculumNature> curriculumNatureList = curriculumNatureRepository.findAll();
        List<AcademicYears> academicYearsList = academicYearsRepository.findAll();
        if (curriculumNatureList.stream().noneMatch(curriculumNature -> curriculumNature.getId().equals(courseAchievement.getCurriculumNature())) &&
                academicYearsList.stream().noneMatch(academicYears -> academicYears.getId().equals(courseAchievement.getAcademicYear()))) {
            return false;
        }
        courseAchievementRepository.save(courseAchievement);
        return true;
    }

    @Override
    @Transactional
    public boolean deleteCourseAchievement(List<String> ids) {

        for (String id : ids) {
            courseAchievementRepository.delete(id);
        }
        return true;
    }

    @Override
    @Transactional
    public int deleteCourseAchievementByStudentId(List<String> studentIds) {
        return courseAchievementRepository.deleteByStudentIdIn(studentIds);
    }


}
